package com.web.hilover.util;

import lombok.extern.slf4j.Slf4j;

import java.security.MessageDigest;
import java.util.Random;

@Slf4j
public class MD5Util {
    public static String encrypt(String source, String salt) {
        try {
            StringBuilder sb = new StringBuilder();
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] bytes = md.digest((source + salt).getBytes("UTF-8"));
            for (byte b : bytes) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();
        } catch (Exception e) {
            log.error("MD5加密失败", e);
            return null;
        }
    }

    public static String generateSalt() {
        Random random = new Random();
        StringBuilder sb = new StringBuilder(16);
        sb.append(random.nextInt(99999999)).append(random.nextInt(99999999));
        int len = sb.length();
        if (len < 16) {
            for (int i = 0; i < 16 - len; i++) {
                sb.append("0");
            }
        }
        return sb.toString();
    }
} 